package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sl.au.entity.Option;
import com.sl.au.entity.ReceivedPaymentTotalInfo;
import com.sl.au.entity.ReceivedPaymentTotalInfoView;
import com.sl.au.entity.User;
import com.sl.au.entity.Module;
import com.sl.au.service.ReceivedPaymentTotalInfoService;
import com.sl.au.service.ReceivedPaymentTotalInfoViewService;
import com.sl.au.entity.Account;
import com.sl.au.service.AttachmentService;
import com.sl.au.service.OperateLogService;
import com.sl.au.service.OptionService;
import com.sl.au.service.PaymentInitialService;


@RestController
@RequestMapping(value = "/ui/ReceivedPaymentTotalInfo")
public class ReceivedPaymentTotalInfoController extends BaseController {
	
	@Autowired
	OptionService oService;
	@Autowired
	ReceivedPaymentTotalInfoService rService;
	@Autowired
	ReceivedPaymentTotalInfoViewService receivedPaymentTotalInfoViewService;
	@Autowired
    PaymentInitialService paymentInitialService;
	@Autowired
	AttachmentService attachmentService;
	@Autowired
	OperateLogService operateLogService;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView index() {		
		ModelAndView modelAndView = createLayoutView("payment/ReceivedPaymentTotalInfoIndex");
		List<Option> options=oService.getRepository().findByGroup("凭证种类");
		Account a = this.getMyAccount();
		String userSuperior=a.getUser().getSaleArea().getSuperior();
		List<User> userList=paymentInitialService.getRepository().getUserList(userSuperior);
		modelAndView.addObject("sReceivedPaymentsIdentifyTypes", options);
		modelAndView.addObject("users", userList);
		return modelAndView;
	}
	
	@RequestMapping(value="getPage",method=RequestMethod.GET)
    public Map<String,Object>getPage(HttpServletRequest request){
    	Map<String,Object>map=new HashMap<>();
    	int start=Integer.parseInt(request.getParameter("start"));
    	int length=Integer.parseInt(request.getParameter("length"));
    	String key=request.getParameter("key");
    	List<ReceivedPaymentTotalInfoView>lists=receivedPaymentTotalInfoViewService.getPage1(start,length,key);
    	long count=receivedPaymentTotalInfoViewService.getCount1(key);
    	map.put("data", lists);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
    	return map;
    }
	
	@RequestMapping(value="getPage1",method=RequestMethod.GET)
    public Map<String,Object>getPage1(HttpServletRequest request){
    	Map<String,Object>map=new HashMap<>();
    	int start=Integer.parseInt(request.getParameter("start"));
    	int length=Integer.parseInt(request.getParameter("length"));
    	String key=request.getParameter("key");
    	List<ReceivedPaymentTotalInfoView>lists=receivedPaymentTotalInfoViewService.getPage(start,length,key);
    	long count=receivedPaymentTotalInfoViewService.getCount(key);
    	map.put("data", lists);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
    	return map;
    }
	
	@RequestMapping(value="newObject",method=RequestMethod.POST)
    public Map<String,Object>newObject(@RequestBody ReceivedPaymentTotalInfo receivedPaymentTotalInfo){
    	Map<String,Object>map=new HashMap<>();
    	
    	Account a=this.getMyAccount();
    	receivedPaymentTotalInfo.setsCreateUserId(a.getUser().getId());
    	int code=this.rService.save(receivedPaymentTotalInfo,a.getUser());
    	
    	map.put("code", code);
    	return map;
    }
	@RequestMapping(value="import",method=RequestMethod.POST)
	public Map<String, Object> importExcel( @RequestParam("file") MultipartFile file, HttpServletRequest request,
			RedirectAttributes redirectAttributes) {

		Map<String, Object> map = new HashMap<String, Object>();

		int code = -1;
		String count = "-1";

		String saveFileName = attachmentService.upload(file);

		Account a=this.getMyAccount();
		User user = a.getUser();
		// 文件传后，再读取EXCEL写入数据库
		if (StringUtils.hasText(saveFileName)) {
			count = rService.importExcel(saveFileName,user);
			code = 0;
		}
		String content  = "";
		operateLogService.log(user, "导入回款凭据", content);
		map.put("code", code);
		map.put("count", count);
		return map;
	}
	@RequestMapping(value="editObject",method=RequestMethod.POST)
    public Map<String,Object>editObject(@RequestBody ReceivedPaymentTotalInfo receivedPaymentTotalInfo){
    	Map<String,Object>map=new HashMap<>();
    	Account a=this.getMyAccount();
    	receivedPaymentTotalInfo.setsCreateUserId(a.getUser().getId());
    	int code=this.rService.patch(receivedPaymentTotalInfo,a.getUser());
    	map.put("code", code);
    	return map;
    }
    @RequestMapping(value="{id}",method=RequestMethod.POST)
    public Map<String,Object>delete(@PathVariable String id){
    	Map<String,Object>map=new HashMap<>();
    	Account a=this.getMyAccount();
    	int code;
		try {
			code = this.rService.delete(id,a.getUser());
		} catch (Exception e) {
			code=1;
			e.printStackTrace();
		}
    	
    	map.put("code", code);
    	return map;
    }
}
